package com.tim.third;

import org.apache.tika.Tika;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.parser.txt.TXTParser;
import org.apache.tika.sax.BodyContentHandler;

import java.io.File;
import java.io.FileInputStream;

/**
 * 通用文档读取。pdf解析，用的pdfbox包
 * tika
 * 官网：https://tika.apache.org/2.9.1/gettingstarted.html
 * demo：https://blog.csdn.net/weixin_42184707/article/details/91045592
 */
public class TikaUtils {

    public static void main(String[] args) {
        String path = "F:\\个人\\王娜\\娜娜资料\\重啤股份2022年报.pdf";
//        parsePdf(path);
        getContext(path);
    }

    // Tika文件类型检测
    public static String getFileType() {
        try {
            File file = new File("D:\\111.zip");
            Tika tika = new Tika();
            String fileType = tika.detect(file);
            if (fileType != null && fileType.contains("/")) {
                fileType = fileType.substring(fileType.indexOf("/") + 1);
            }
            return fileType;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // Tika内容提取
    public static String getContext(String filePath) {
        try {
            File file = new File(filePath);
            Tika tika = new Tika();
            tika.setMaxStringLength(Integer.MAX_VALUE);
            String filecontent = tika.parseToString(file);
            System.out.println("Extracted Content: " + filecontent);
            return filecontent;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // Tika元数据提取
    public static String getMetadata() {
        try {
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(new File("D:\\111.pdf"));
            ParseContext pcontext = new ParseContext();
            //parsing the document using PDF parser
            PDFParser pdfparser = new PDFParser();
            pdfparser.parse(inputstream, handler, metadata, pcontext);
            //getting the content of the document
            System.out.println("Contents of the PDF :" + handler.toString());
            // 元数据提取
            System.out.println("Metadata of the PDF:");
            String[] metadataNames = metadata.names();
            for (String name : metadataNames) {
                System.out.println(name + " : " + metadata.get(name));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // Tika语言检测
//    public static String LanguageDetection() {
//        try {
//            Parser parser = new AutoDetectParser();
//            BodyContentHandler handler = new BodyContentHandler();
//            Metadata metadata = new Metadata();
//            FileInputStream content = new FileInputStream(new File("D:\\111.zip"));
//            //Parsing the given document
//            parser.parse(content, handler, metadata, new ParseContext());
//            LanguageIdentifier object = new LanguageIdentifier(handler.toString());
//            System.out.println("Language name :" + object.getLanguage());
//            return object.getLanguage();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "";
//    }

    // Tika提取pdf文件
    public static String parsePdf(String path) {
        try {
            BodyContentHandler handler = new BodyContentHandler(Integer.MAX_VALUE);
            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(new File(path));
            ParseContext pcontext = new ParseContext();
            //parsing the document using PDF parser
            PDFParser pdfparser = new PDFParser();
            pdfparser.parse(inputstream, handler, metadata, pcontext);
            //getting the content of the document
            System.out.println("Contents of the PDF :" + handler.toString());
            // 元数据提取
            System.out.println("Metadata of the PDF:");
            String[] metadataNames = metadata.names();
            for (String name : metadataNames) {
                System.out.println(name + " : " + metadata.get(name));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // Tika提取文本文档
    public static String parseTxt() {
        try {
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(new File("D:\\example.txt"));
            ParseContext pcontext = new ParseContext();
            //Text document parser
            TXTParser TexTParser = new TXTParser();
            TexTParser.parse(inputstream, handler, metadata, pcontext);
            System.out.println("Contents of the document:" + handler.toString());
            return handler.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // Tika提取excel内容
    public static String parseExcel(){
        try {
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(new File("D:\\111.xlsx"));
            ParseContext pcontext=new ParseContext();
            OOXMLParser msofficeparser = new OOXMLParser ();
            msofficeparser.parse(inputstream, handler, metadata,pcontext);
            System.out.println("Contents of the document:" + handler.toString());
            return handler.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    // Tika提取word内容
    // Tika-1.*最高支持2007及更低版本的Office Word文档，如果是高于2007版本的Word文档需要使用POI处理（Tika会报错）

}
